import os
import cv2
import numpy as np


def downsample_image(image, scale=0.125):
    # 下采样图片
    downsampled_image = cv2.resize(image, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_LANCZOS4)
    return downsampled_image


def batch_downsample_large_image(input_dir, output_dir, downscale=0.125):
    # 创建输出目录（如果不存在）
    os.makedirs(output_dir, exist_ok=True)

    image_files = [f for f in os.listdir(input_dir) if f.endswith('.png')]

    for image_file in image_files:
        input_path = os.path.join(input_dir, image_file)
        image = cv2.imread(input_path)
        if image is None:
            print(f"无法打开图像文件：{input_path}")
            continue

        # 下采样图片
        downsampled_image = downsample_image(image, downscale)

        # 生成输出文件路径
        output_path = os.path.join(output_dir, image_file)

        # 保存下采样后的图片
        cv2.imwrite(output_path, downsampled_image)
        print(f"Processed {image_file} and saved to {output_path}")


# 示例用法
input_dir = 'E:\\wafer52\\11867_256nm'  # 替换为实际输入目录路径
output_dir = 'E:\\wafer52\\11867_2048nm'  # 替换为实际输出目录路径

batch_downsample_large_image(input_dir, output_dir, downscale=0.125)

print("Batch downsampling to 2048nm completed.")



